package filescript;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: ycy
 * @CreateDate: 2019/1/11 12:10
 */
public class FileScripts {

  public static void main(String[] args) {
//黏著稱=黏著称  黏著稱=黏著称    \u9ed2 \u9ed1
    System.out.println('\u9ed2');
    System.out.println('\u9ed1');
/*    String src = filterKeyword("䰾");
    System.out.println(src);
    System.out.println(ZHConverter.trad2simp(src));
*/

    FileScripts fileScripts = new FileScripts();
//    fileScripts.getChinaChar("D:\\Users\\yincy\\Desktop\\日志\\繁.txt");


    /*Set<String> chinaCharOne = fileScripts.getChinaCharOne("D:\\Users\\yincy\\Desktop\\日志\\繁(只有中文).txt");
    for (String s : chinaCharOne) {
      System.out.println(s);
    }*/

    //对比
//    fileScripts.completed("D:\\Users\\yincy\\Desktop\\日志\\繁体(只有中文单字).txt","D:\\Users\\yincy\\Desktop\\日志\\简体(只有中文单字).txt");

    Map<String, String> completed = fileScripts.completed("D:\\Users\\yincy\\Desktop\\日志\\繁体(只有中文单字).txt", "D:\\Users\\yincy\\Desktop\\日志\\简体(只有中文单字).txt");

    fileScripts.readLocalProperties("D:\\Users\\yincy\\Desktop\\日志\\zh2Hans.properties");
    System.out.println("==================================================");
    for (Map.Entry<String, String> entry : completed.entrySet()) {
      String key = entry.getKey();
      if(!fileScripts.charMap.containsKey(key)){
        System.out.println(entry.getValue());
      }
    }

  }


  /**
   * 提取中文
   *
   * @param filePath
   * @return
   */
  public Map<String, String> getChinaChar(String filePath) {
    //hotelid,标签集合
    Map<String, String> map = Maps.newHashMap();
    File file = new File(filePath);
    FileInputStream fis = null;
    BufferedReader br = null;
    try {
      Long start = System.currentTimeMillis();
      fis = new FileInputStream(file);
      br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
      while (br.ready()) {
        String filter = filter(br.readLine());
        if (StringUtils.isNotBlank(filter)) {
          System.out.println(filter);
        }
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {

    }
    return map;
  }

  /**
   * 提取中文
   *
   * @param filePath
   * @return
   */
  public Set<String> getChinaCharOne(String filePath) {
    //hotelid,标签集合
    Set<String> set = Sets.newHashSet();
    File file = new File(filePath);
    FileInputStream fis = null;
    BufferedReader br = null;
    try {
      Long start = System.currentTimeMillis();
      fis = new FileInputStream(file);
      br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
      while (br.ready()) {
        String str = br.readLine();
        for (int i = 0; i < str.length(); i++) {
          char c = str.charAt(i);
          String key = "" + c;
          set.add(key);
        }

      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {

    }
    return set;
  }


  /**
   * 提取中文
   *
   * @param filePath
   * @return
   */
  public List<String> getChinaCharOneList(String filePath) {
    //hotelid,标签集合
    List<String> list = Lists.newArrayList();
    File file = new File(filePath);
    FileInputStream fis = null;
    BufferedReader br = null;
    try {
      Long start = System.currentTimeMillis();
      fis = new FileInputStream(file);
      br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
      while (br.ready()) {
        String str = br.readLine();
        for (int i = 0; i < str.length(); i++) {
          char c = str.charAt(i);
          String key = "" + c;
          list.add(key);
        }

      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {

    }
    return list;
  }

  public Map<String,String> completed(String filePath1, String filePath2) {
    Map<String,String> map = Maps.newHashMap();

    List<String> chinaCharOneList1 = getChinaCharOneList(filePath1);
    List<String> chinaCharOneList2 = getChinaCharOneList(filePath2);

    for (int i = 0; i < chinaCharOneList1.size(); i++) {
      String cs1 = chinaCharOneList1.get(i);
      String cs2 = chinaCharOneList2.get(i);
      if (!StringUtils.equals(cs1, cs2)) {
        String x = cs1 + "=" + cs2;
        System.out.println(x);
        map.put(cs1,x);
      }
    }
    return map;

  }

  private Properties charMap = new Properties();

  public void readLocalProperties(String propertyFile) {
    InputStream is = null;
    //File propertyFile = new File("C:/Temp/testMDB/TestTranslator/abc.txt");
    BufferedReader reader = null;
    try {
      is = new FileInputStream(propertyFile);
      reader = new BufferedReader(new InputStreamReader(is));
      charMap.load(reader);
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (reader != null)
          reader.close();
        if (is != null)
          is.close();
      } catch (IOException e) {
      }
    }
      int i = 0;
    for (Map.Entry<Object, Object> objectObjectEntry : charMap.entrySet()) {
      System.out.println((++i) +" " + objectObjectEntry.getKey() + " " + objectObjectEntry.getValue());
    }


  }

  static String reg = "[^\u4e00-\u9fa5]";

  /**
   * 提取中文
   */
  public static String filter(String str) {
    str = str.replaceAll(reg, "");
    return str;
  }

  /**
   * 特殊字符過濾
   */
  private static final String SPECIAL_CHAR_REG = "[\\\\`'~!@#$%^&�*()=•|{}:;\\[\\].<>?~！@#￥……&*（）——|{}【】‘；：”“’。，、？￼\n\t]";
  private static final Pattern SPECIAL_CHAR_PATTERN = Pattern.compile(SPECIAL_CHAR_REG);

  /**
   * 搜索词过滤
   */
  public static String filterKeyword(String keyword) {
    keyword = keyword.trim().replaceAll("\"", " ");
    keyword = keyword.replaceAll(" +", " ");
    if (!hasEnglish(keyword)) {
      keyword = keyword.replaceAll(" ", "");
    }
    Matcher m = SPECIAL_CHAR_PATTERN.matcher(keyword);
    return m.replaceAll("").trim();
  }


  /**
   * 有英文关键词
   */
  public static boolean hasEnglish(String keyword) {
    if (StringUtils.isEmpty(keyword)) {
      return false;
    }

    keyword = keyword.replaceAll("\\s", "").toLowerCase();
    for (char c : keyword.toCharArray()) {
      if (c >= 'a' && c <= 'z') {
        return true;
      }
    }
    return false;
  }

}
